"""GO Analysis

.. helpdoc::
"""

"""<widgetXML>
    <name>GO Analysis</name>
    <icon>normalize.png</icon>
    <tags> 
        <tag>Microarray</tag> 
    </tags>
    <summary></summary>
    <author>
        <authorname>Anup Parikh</authorname>
        <authorcontact>anup@red-r.org</authorcontact>
    </author>
</widgetXML>
"""

"""
<name>GO Analysis</name>
<tags>Microarray</tags>
<icon>normalize.png</icon>
"""

from OWRpy import *
import globalData

from libraries.base.signalClasses.RDataFrame import RDataFrame as redRRDataFrame
from libraries.base.signalClasses.RList import RList as redRRList
from libraries.base.signalClasses.RVector import RVector as redRRVector
from libraries.affy.signalClasses.RTopGOData import RTopGOData as redRRTopGOData


from libraries.base.qtWidgets.groupBox import groupBox as redRGroupBox
from libraries.base.qtWidgets.widgetBox import widgetBox as redRWidgetBox
from libraries.base.qtWidgets.commitButton import commitButton as redRCommitButton
from libraries.base.qtWidgets.comboBox import comboBox as redRComboBox
from libraries.base.qtWidgets.radioButtons import radioButtons as redRRadioButtons
from libraries.base.qtWidgets.checkBox import checkBox as redRcheckBox
from libraries.base.qtWidgets.spinBox import spinBox as redRspinBox
from libraries.base.qtWidgets.lineEdit import lineEdit as redRlineEdit
from libraries.base.qtWidgets.widgetLabel import widgetLabel as redRwidgetLabel

class redRGOanalysis(OWRpy):
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)


        self.geneList = None
        self.chipType = None
        self.ontologies = ["BP", "MF", "CC"]
        self.setRvariableNames(['tests','results'])

        self.require_librarys(['topGO','ALL'])
        
        """.. rrsignals::"""
        self.inputs.addInput("id1", "TopGO Data", redRRTopGOData, self.process)
        
        """.. rrsignals::"""
        self.outputs.addOutput('od1', 'GO Terms Table', redRRDataFrame) #[("data.frame", rdf.RDataFrame)]
        
        """.. rrsignals::"""
        self.outputs.addOutput('od2', 'Test Results', redRRList) #[("data.frame", rdf.RDataFrame)]

        
        testBox = redRGroupBox(self.controlArea,label='Run Tests')
        self.algorithm = redRComboBox(testBox,label='Algorithm',
        items=['classic','elim','weight01','lea'])
        
        self.test = redRComboBox(testBox,label='Test',
        items=['fisher','ks','t','globaltest','sum'])
        
        redRCommitButton(self.bottomAreaRight,label='Run Test',callback=self.commitFunction)

    def process(self,data):
        if not data: return
        self.topGOData = data.getData()
        print 'aaaaaaaaaaaaaaaaaa', self.topGOData
    def commitFunction(self):
        algorithm = self.algorithm.currentText()
        test = self.test.currentText()
        
        ontologies = self.R('names(%s)'% self.topGOData,wantType='list')
        self.R('%s = NULL' % self.Rvariables['results'])
        self.R('%s = NULL' % self.Rvariables['tests'])
        
        for ontology in ontologies:
            self.R('%s$%s <- runTest(%s[["%s"]],algorithm="%s",statistic="%s")' % (
            self.Rvariables['tests'],ontology,
            self.topGOData,ontology, algorithm,test))
            
            self.R('%s <- rbind(%s,cbind(ontology="%s",GenTable(%s$%s, %s_%s = %s$%s)))' % (
            self.Rvariables['results'],self.Rvariables['results'],ontology,
            self.topGOData,ontology, algorithm,test,self.Rvariables['tests'],ontology))
        
        
        sendData = redRRDataFrame(data = self.Rvariables['results'])
        self.rSend("od1", sendData)
        sendData = redRRList(data = self.Rvariables['tests'], parent = self.topGOData)
        self.rSend("od2", sendData)

        
        
        